package com.sad.we11cis;

import java.util.ArrayList;
import java.util.Collections;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class ReviewList extends Activity {
	
	private ListView reviewListView = null;
	private ListViewAdapter revLvAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_review_list);
		
		Intent intent = getIntent();
		
		String name = intent.getSerializableExtra("ProductName").toString();
		
		
		reviewListView = (ListView) findViewById(R.id.revListView);
		
        
		revLvAdapter = new ListViewAdapter(this);
		reviewListView.setAdapter(revLvAdapter);
        
		revLvAdapter.addItem(name+" Review_1","Review Description Here");
        
		revLvAdapter.addItem(name+" Review_2","Review Description Here");
        
		revLvAdapter.addItem(name+" Review_3","Review Description Here");
        
		revLvAdapter.addItem(name+" Review_4","Review Description Here");
		
		reviewListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ListData mData = revLvAdapter.mListData.get(arg2);
				Toast.makeText(ReviewList.this, mData.Title, Toast.LENGTH_SHORT).show();
			}
			
		});
	}

	private class ViewHolder{
		
		public TextView mText;
		
		public TextView mDate;
	}
	
	private class ListViewAdapter extends BaseAdapter{

		private Context context = null;
		private ArrayList<ListData> mListData = new ArrayList<ListData>();
		
		public ListViewAdapter(Context context){
			super();
			this.context= context;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mListData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		public void addItem(String mTitle, String mDate){
		    ListData addInfo = null;
		    addInfo = new ListData();
		    addInfo.Title = mTitle;
		    addInfo.Date = mDate;
		             
		    mListData.add(addInfo);
		}
		 
		public void remove(int position){
		    mListData.remove(position);
		    dataChange();
		}
		 
		public void sort(){
		    Collections.sort(mListData, ListData.ALPHA_CO_COMPARATOR);
		    dataChange();
		}
		 
		public void dataChange(){
			revLvAdapter.notifyDataSetChanged();
		}
		
		@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.listview_item, null);
                
                holder.mText = (TextView) convertView.findViewById(R.id.productName);
                holder.mDate = (TextView) convertView.findViewById(R.id.productDesc);
                
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            
            ListData Data = mListData.get(position);
            
            holder.mText.setText(Data.Title);
            holder.mDate.setText(Data.Date);
            
            return convertView;
        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.review_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_review_list,
					container, false);
			return rootView;
		}
	}

}